package com.dongdongshop.controller;

import com.dongdongshop.data.PageResult;
import com.dongdongshop.data.PageUtil;
import com.dongdongshop.data.Result;
import com.dongdongshop.domain.vo.TypeTemplateVO;
import com.dongdongshop.service.TypeTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("type")
@CrossOrigin
@Api("运营商后台管理——模板管理")
@RefreshScope   //自动监听配置文件
@Slf4j
public class TypeTemplateController {

    @Autowired
    private TypeTemplateService typeTemplateService;

    //分页+模糊查询
    @PostMapping("selectAll")
    @ApiOperation("分页+模糊查询")
    @ApiImplicitParam(name = "parms", value = "分页工具类", required = true, paramType = "body")
    public Result selectAll(@RequestBody PageResult<TypeTemplateVO> parms){
        PageUtil<TypeTemplateVO> page = typeTemplateService.selectAll(parms);
        return Result.OK().setData(page);
    }

    //增加
    @PostMapping("add")
    @ApiOperation("增加模板")
    @ApiImplicitParam(name = "templateVO", value = "模板类", required = true, paramType = "body")
    public Result addTemplate(@RequestBody TypeTemplateVO templateVO){
        typeTemplateService.addTemplate(templateVO);
        return Result.OK();
    }

    //根据id查询/回显
    @PostMapping("selectById")
    @ApiOperation("根据id查询/回显")
    @ApiImplicitParam(name = "id", value = "模板主键id", required = true, paramType = "query")
    public Result selectById(@RequestParam Long id){
        TypeTemplateVO vo = typeTemplateService.selectById(id);
        return Result.OK().setData(vo);
    }

    //修改
    @PutMapping("update")
    @ApiOperation("修改模板")
    @ApiImplicitParam(name = "templateVO", value = "模板类", required = true, paramType = "body")
    public Result updateTemplate(@RequestBody TypeTemplateVO templateVO){
        typeTemplateService.updateTemplate(templateVO);
        return Result.OK();
    }

    //批量删除
    @DeleteMapping("delete")
    @ApiOperation("批量删除")
    @ApiImplicitParam(name = "ids", value = "模板主键id的list集合", required = true, paramType = "body")
    public Result delete(@RequestBody List<Long> ids){
        typeTemplateService.delete(ids);
        return Result.OK();
    }

    //根据模板id查询数据
    @PostMapping("getCustomById")
    public Result getCustomById(@RequestParam Long id) {
        TypeTemplateVO templateVo = typeTemplateService.getByItemCatId(id);
        return Result.OK().setData(templateVo);
    }

}
